package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Questionnaire;
import com.itheima.service.QuestionnaireService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/questionnaire")
public class QuestionnaireController {

    @Reference
    private QuestionnaireService questionnaireService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = questionnaireService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());

        return pageResult;
    }

    //新建
    @RequestMapping("/add")
    public Result add(@RequestBody Questionnaire questionnaire) {

        try {
            questionnaireService.add(questionnaire);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_QUESTIONNAIRE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_QUESTIONNAIRE_SUCCESS);
    }

    //修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Questionnaire questionnaire) {

        try {
            questionnaireService.edit(questionnaire);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_QUESTIONNAIRE_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_QUESTIONNAIRE_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {

        return questionnaireService.findById(id);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            questionnaireService.delete(id);
            return new Result(true, MessageConstant.DELETE_QUESTIONNAIRE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_QUESTIONNAIRE_FAIL);
        }
    }
}
